package com.oms.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.liudecai.utils.jdbc.TxQueryRunner;
import com.oms.bean.Food;

public class FoodDao {
	QueryRunner qr=null;
	public FoodDao() {
		qr=new TxQueryRunner();
	}

	public void Add(Food food) throws SQLException {
		String sql = "insert into oms_food (food_id,category_id,food_name,market_price,market_price_sale,vip_price,vip_price_sale,image,material,description) values(?,?,?,?,?,?,?,?,?,?)";
		qr.update(sql,food.getFood_id(),food.getCategory_id(),food.getFood_name(),food.getMarket_price(),food.getMarket_price_sale(),food.getVip_price(),food.getVip_price_sale(),food.getImage(),food.getMaterial(),food.getDescription());
	}

	public void Delete(int id) throws SQLException {
		String sql = "delete from oms_food where id=?";
		qr.update(sql, id);
	}

	public void Update(Food food) throws SQLException {
		String sql = "update oms_food set food_id=?,category_id=?,food_name=?,market_price=?,market_price_sale=?,vip_price=?,vip_price_sale=?,image=?,material=?,description=? where id=?";
		qr.update(sql, food.getFood_id(),food.getCategory_id(),food.getFood_name(),food.getMarket_price(),food.getMarket_price_sale(),food.getVip_price(),food.getVip_price_sale(),food.getImage(),food.getMaterial(),food.getDescription(),food.getId());
	}

	public Food FindById(int id) throws SQLException {
		String sql = "select * from oms_food where id=?";
		return qr.query(sql, new BeanHandler<Food>(Food.class),id);

	}

	public List<Food> FindByString(String str) throws SQLException {
		String sql="select * from oms_food S where Concat(S.*) like '%"+str+"%'";
		List<Food> foods=qr.query(sql, new BeanListHandler<Food>(Food.class));
		return (List<Food>) foods;

	}

	public List<Food> FindAll() throws SQLException {
		String sql="select * from oms_food";
		List<Food> foods=qr.query(sql, new BeanListHandler<Food>(Food.class));
		return (List<Food>) foods;
	}

	public long FoodCount() throws SQLException {
		String sql="select count(id) from oms_food";
		return qr.query(sql, new ScalarHandler<Long>());
	}

}
